package org.litespring.aop.aspectj;

import org.litespring.aop.Advice;
import org.litespring.aop.config.AspectInstanceFactory;
import org.litespring.aop.Pointcut;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author ChangLiang
 * @date 2020/8/25
 */
public abstract class AbstractAspectJAdvice implements Advice {

    private Method adviceMethod;

    private Pointcut pc;

    private AspectInstanceFactory aspectInstanceFactory;

    public AbstractAspectJAdvice(Method adviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aspectInstanceFactory) {
        this.adviceMethod = adviceMethod;
        this.pc = pointcut;
        this.aspectInstanceFactory = aspectInstanceFactory;
    }

    @Override
    public Pointcut getPointcut() {
        return this.pc;
    }

    public Method getAdviceMethod() {
        return this.adviceMethod;
    }

    public void invokeAdviceMethod() throws InvocationTargetException, IllegalAccessException {
        adviceMethod.invoke(this.aspectInstanceFactory.getAspectInstance());
    }

    public Object getAdviceInstance() {
        return aspectInstanceFactory.getAspectInstance();
    }
}
